﻿using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Actions;
using CardPlay.Model.GameModel.Presentation;
using CardPlay.Model.Layout;

namespace CardPlay.DemoPack.Games.CongressVariants
{
    [Game("73DFD665-EDF1-4610-95BB-6EDAE10C5948", LevelOfDifficulty.Easy, LevelOfSkill.MostlyChance, DurationOfPlay.MoreThan10Minutes, NumberOfDecks = 2, Family = FamilyName)]
    public class Congress : StandardCardGame
    {
        public const string FamilyName = "Congress";

        public Congress()
        {
            NamesOfAdditionalPiles.AddRange(new[] { Stock, Waste });

            CardsPerDeal = 1;
            TotalDecks = 2;
            TotalTableaus = 8;
            TotalFoundations = 8;

            TableauBuildSequence = CardFaceSequence.AlternatingColorDescendingRank;

            DefaultTableauStyle = StandardStyles.Tableau;
        }

        public override ILayoutSite CreatePresentation(IPresentationContext presentation)
        {
            var foundations = presentation.CreateRowLayout();
            //presentation.AddFoundationPiles(this, foundations, FoundationBaseRank);
            presentation.AddPiles(foundations, NamesOfFoundations, presentation.StyleForRepeatedRankFoundation(this, FoundationBaseRank, TotalDecks));

            var tableau = presentation.CreateRowLayout();
            presentation.AddPiles(tableau, PilesNamed(AnyTableau), DefaultTableauStyle);

            var root = new GridLayout
            {
                Columns = 3,
                Rows = 2,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Stretch
            };

            root.Add(presentation.CreatePileVisual(Stock, presentation.StyleForStock(this)));
            root.Add(presentation.CreateMimimalSpacer());
            root.Add(foundations);

            root.Add(presentation.CreatePileVisual(Waste, presentation.StyleForWaste()));
            root.Add(presentation.CreateMimimalSpacer());
            root.Add(tableau);

            return root;
        }

        protected VisualTraits DefaultTableauStyle
        {
            get; set;
        }

        protected override void PrepareDealScript(DealScript script)
        {
            script.DealCardToEach(NamesOfTableaus);
            script.DealRemainingFaceDownTo(Stock);
        }

        protected virtual bool IsTableauPickupIsAllowed(RemovePileAction action)
        {
            return action.IsActingOnFaceUpTopCard();
        }

        protected override bool AllowedToBuildFoundation(MovePileAction action)
        {
            return action.IsBuildingTwoDeckFoundationWithIdenticalBase();
        }

        protected override bool AllowedToBuildEmptyTableau(MovePileAction action)
        {
            return IsValidBuildCard(action.PileBeingMoved.BottomCard) && action.FromLocation.Matches(Waste);
        }

        protected override bool AllowedToBuildNonEmptyTableau(MovePileAction action)
        {
            return IsValidBuildCard(action.PileBeingMoved.BottomCard) && base.AllowedToBuildNonEmptyTableau(action);
        }

        protected override void OnTransactionEnding()
        {
            this.FillFromStockEmptyPilesMatching(AnyTableau);
        }

        protected bool IsValidBuildCard(CardFace face)
        {
            return face.Rank != FoundationBaseRank && face.Rank != Rank.King;
        }
    }
}